package JAVA300.第十一章多线程;

/**
 * 观察线程的状态
 */
public class thread_11_threadState_getState {
    public static void main(String[] args) {
        Thread t = new Thread (()->{

            for (int i= 0;i<5;i++){

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("aaaaaaaaaaaaa");
            }


        });
        Thread.State state = t.getState();
        System.out.println(state.toString());//NEW

        t.start();
        state = t.getState();
        System.out.println(state.toString());//RUNNABLE

        //循环监控1

//        while (state != Thread.State.TERMINATED){
//
//            try {
//                Thread.sleep(200);
//                state = t.getState();
//                System.out.println(state.toString());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//        }
        //循环监控2 利用线程数
        while (true){

            int num= Thread.activeCount();
            System.out.println(num);
            if (num ==2){
                break;
            }
            try {
                Thread.sleep(200);
                state = t.getState();
                System.out.println(state.toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }
}
